﻿//_______________________________________________________________________________________________________________________
// Copy Right (c) 2010, 2011 SapiensIT Consulting GmbH, all rights reserved
//_______________________________________________________________________________________________________________________
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//_______________________________________________________________________________________________________________________
// PROJECT:     sapiens.at.SharePoint (SharePoint 2010/V4)
//_______________________________________________________________________________________________________________________
// VERSION:     2.0
//_______________________________________________________________________________________________________________________
// DESCRIPTION: 
//_______________________________________________________________________________________________________________________
// LICENSE:     Different licens Types apply to different file types:
//               - Source code files are governed by the MICROSOFT PUBLIC LICENSE (Ms-PL)
//               - Binary files are governed by MSDN CODE GALLERY BINARY LICENSE
//               - Documentation files are governed by CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
//               The licenses can be found at the projects source location.
//               If you do not agree, don't use it!
// 
//_______________________________________________________________________________________________________________________
// Start SharePoint Branding, visit http://www.cliggin.com! Visit us on http://www.sapiens.at! Tell your friends! 
//_______________________________________________________________________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using System.Collections.Specialized;
using System.IO;

namespace Sapiens.at.SharePoint.Receiver
{
    public class BaseMailReceiver : SPEmailEventReceiver
    {
        #region Properties
        private string _subject;
        public string Subject { get { return _subject; } }
        private string _sender;
        public string Sender { get { return _sender; } }
        private StringCollection _to = new StringCollection();
        public StringCollection TO { get { return _to; } }
        private StringCollection _cc = new StringCollection();
        public StringCollection CC { get { return _cc; } }
        private byte[] _message;
        public byte[] Message { get { return _message; } }
        public string Body
        {
            get
            {
                return System.Text.Encoding.ASCII.GetString(_message);
            }
        }
        #endregion

        public override void EmailReceived(SPList list, Microsoft.SharePoint.Utilities.SPEmailMessage emailMessage, string receiverData)
        {
            base.EmailReceived(list, emailMessage, receiverData);
            _subject = Convert.ToString(emailMessage.Headers["Subject"]);
            _sender = emailMessage.Sender;
            string TOString = Convert.ToString(emailMessage.Headers["To"]);
            if (!string.IsNullOrEmpty(TOString))
                while (TOString.Contains("<"))
                {
                    string to = TOString.Substring(TOString.IndexOf("<") + 1);
                    to = to.Substring(0, to.IndexOf(">"));
                    _to.Add(to);
                    TOString = TOString.Substring(TOString.IndexOf(">") + 1);
                }
            string CCString = Convert.ToString(emailMessage.Headers["Cc"]);
            if(!string.IsNullOrEmpty(CCString))
                while (CCString.Contains("<"))
                {
                    string cc = CCString.Substring(CCString.IndexOf("<") + 1);
                    cc = cc.Substring(0, cc.IndexOf(">"));
                    _cc.Add(cc);
                    CCString = CCString.Substring(CCString.IndexOf(">") + 1);
                }
            Stream messageStream = emailMessage.GetMessageStream();
            _message = new byte[messageStream.Length];
            emailMessage.GetMessageStream().Read(_message, 0, _message.Length);
        }
    }
}
